﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierService;
using TierApplication;
using System.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxGridView.Rendering;
using System.Web.Services;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses.Internal;

public partial class System_Order_OrderList : BasePage
{
    #region private
    private const string inputQueryStringPageModeKey = "pagemode";
    private const string currentPageModeKey = "CurrentPageMode";
    public enum PageMode
    {
        add,
        update,
        query,
        select
    }
    private Nullable<PageMode> pm;


    public enum enuDisplayMode
    {
        Add = 0,
        Edit = 1,
        Delete = 2,
        Init = 3
    }

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindOrderStatus();
            BindSchool();

            string sModuleID = Page.Request.QueryString["MOUDLEID"];
            string pagemode = Page.Request.QueryString[inputQueryStringPageModeKey];
            if (pagemode == null) pagemode = "";
            switch (pagemode.ToLower().Trim())
            {
                case "add":
                    pm = PageMode.add;
                    break;
                case "update":
                    pm = PageMode.update;
                    break;
                case "query":
                    pm = PageMode.query;
                    break;
                case "select":
                    pm = PageMode.select;
                    break;
                default:
                    pm = PageMode.add;
                    break;
            }
            ViewState[currentPageModeKey] = pm;

            BindData();
        }
        else
        {
            pm = ViewState[currentPageModeKey] as Nullable<PageMode>;
            if (gridSearchResult.IsCallback)
            {
                System.Data.DataTable dt = (DataTable)ViewState["data"];
                gridSearchResult.DataSource = dt;
                gridSearchResult.DataBind();
            }
        }


    }

    #region BindData

    private void BindOrderStatus()
    {
        DataSet ds = P_DDCODE.GetListByType(P_DDCODE.DDType.OrderStatus.ToString());
        this.ddlORDERSTATUS.DataSource = ds.Tables[0].DefaultView;
        this.ddlORDERSTATUS.TextField = "CodeName";
        this.ddlORDERSTATUS.ValueField = "CodeID";
        this.ddlORDERSTATUS.DataBind();
        this.ddlORDERSTATUS.Items.Insert(0, new ListEditItem("(全部)", ""));
    }


    private void BindSchool()
    {
        T_SCHOOL mSchool = new T_SCHOOL();
        DataSet ds = mSchool.GetList("");
        this.ddlSCHOOLID.DataSource = ds;
        this.ddlSCHOOLID.ValueField = "SCHOOLID";
        this.ddlSCHOOLID.TextField = "SCHOOLNAME";
        this.ddlSCHOOLID.DataBind();
        this.ddlSCHOOLID.Items.Insert(0, new ListEditItem("(全部)", ""));
    }

    private void BindData()
    {
        #region 权限检查

        #endregion

        string ORDERID = this.txtORDERID.Text.Trim();
        string ORDERDATE_START = this.txtORDERDATE_START.Text.Trim();
        string ORDERDATE_END = this.txtORDERDATE_END.Text.Trim();
        string SCHOOLID = (string)this.ddlSCHOOLID.SelectedItem.Value;
        string ORDERSTATUS = (string)this.ddlORDERSTATUS.SelectedItem.Value;
        string MOBILE = this.txtMOBILE.Text.Trim();
        string ACCOUNTID = this.txtACCOUNTID.Text.Trim();
        string ACCOUNTNAME = this.txtACCOUNTNAME.Text.Trim();
        string strWhere = "";

        if (ORDERID != "")
        {
            strWhere += " and A.ORDERID like'%" + ORDERID + "%'";
        }
        if (SCHOOLID != "")
        {
            strWhere += " and B.SCHOOLID ='" + SCHOOLID + "'";
        }
        if (ORDERSTATUS != "")
        {
            strWhere += " and A.ORDERSTATUS ='" + ORDERSTATUS + "'";
        }
        if ((ORDERDATE_START != "")&&(ORDERDATE_END!=""))
        {
            strWhere += " and TO_CHAR(A.ORDERDATE,'YYYY-MM-DD')>='" + this.txtORDERDATE_START.Date.ToString("yyyy-MM-dd") + "'";
            strWhere += " and TO_CHAR(A.ORDERDATE,'YYYY-MM-DD')<='" + this.txtORDERDATE_END.Date.ToString("yyyy-MM-dd") + "'";
        }

        if (MOBILE != "")
        {
            strWhere += " and B.MOBILE like'%" + MOBILE + "%'";
        }

        if (ACCOUNTNAME != "")
        {
            strWhere += " and B.ACCOUNTNAME like'%" + ACCOUNTNAME + "%'";
        }

        if (ACCOUNTID != "")
        {
            strWhere += " and B.ACCOUNTID like'%" + ACCOUNTID + "%'";
        }


        T_ORDER mOrder = new T_ORDER();
        DataSet ds = mOrder.SP_GET_ORDERLIST(strWhere);
        gridSearchResult.DataSource = ds.Tables[0];
        gridSearchResult.DataBind();
        ViewState["data"] = ds.Tables[0];
    }
    #endregion

    #region btn_Search

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnQuery_CallBack(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
        BindData();
    }



    #endregion

    #region gridView事件
    protected void gridSearchResult_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {

        String theID = e.Keys[0].ToString();
        object theValues = this.gridSearchResult.GetRowValuesByKeyValue(theID, new String[] { "PRODUCTID" });
        String ProductID = theValues.ToString();
        if (!String.IsNullOrEmpty(ProductID))
        {
            T_PRODUCT mProduct = new T_PRODUCT();
            mProduct.Delete(ProductID);
        }
        e.Cancel = true;
        this.gridSearchResult.CancelEdit();
        BindData();
    }


    private GridViewCommandColumn getCommandColumnLoop()
    {
        GridViewCommandColumn theCommandColumn = null;
        foreach (GridViewColumn item in gridSearchResult.VisibleColumns)
        {
            if (item.GetType() == typeof(GridViewCommandColumn))
            {
                theCommandColumn = (GridViewCommandColumn)item;
                break;
            }
        }
        return theCommandColumn;
    }

    #endregion

}